﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

class SearchFiles
{
    public static string searchFor;
    public static Dictionary<string, string> results;
    public static string SearcInFileFor(FileInfo fileName)
    {
        StreamReader reader = new StreamReader(fileName.FullName);
        using(reader)
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains(searchFor))
                {
                    return line;
                }
            }
        }
        return "";
    }
    public static void CheckAllFiles(DirectoryInfo dir)
    {
        foreach (var file in dir.GetFiles())
        {
            if (file.Name.EndsWith(".txt"))
            {
                string line = SearcInFileFor(file);
                if (line != "")
                {
                    results.Add(file.FullName, line);
                }
            }
        }
        foreach (var directory in dir.GetDirectories())
        {
            CheckAllFiles(directory);
        }
    }
    public static void PrintResults()
    {
        StreamWriter writer = new StreamWriter("result.txt");
        using (writer)
        {
            foreach (var res in results)
            {
                writer.Write(res.Key);
                writer.Write("  ");
                writer.WriteLine(res.Value);
            }
        }
    }

    static void Main(string[] args)
    {
        searchFor = "zarko";
        results = new Dictionary<string, string>();
        string name = @"C:\SomeFolder";
        DirectoryInfo dir = new DirectoryInfo(name);
        CheckAllFiles(dir);
        PrintResults();
   }
}

